package utils

import (
	"fmt"
	"github.com/gogf/gf/util/gconv"
	"os"
)

// 文件是否存在
func IsFileExists(name string) bool {
	if _, err := os.Stat(name); err != nil {
		if os.IsNotExist(err) {
			return false
		}
	}
	return true
}

// 是否是文件
func IsFileOrSize(f string) (bool, int64) {
	fi, e := os.Stat(f)
	if e != nil {
		return false, 0
	}
	return !fi.IsDir(), fi.Size()
}

// 获得是否过滤字符串
func IsFilterStr(str string, filterStrList []string) bool {
	for _,filterStr := range filterStrList {
		if strings.Index(str, filterStr) != -1 {
			return true
		}
	}
	return false
}

// 获得计算文件转换
func GetCalcTotalSize(totalSize int64)string {
	if (totalSize >= 1e9) {
		return fmt.Sprintf("%.2fGB", gconv.Float32(totalSize)/1e9)
	}
	if (totalSize >= 1e6) {
		return fmt.Sprintf("%.2fMB", gconv.Float32(totalSize)/1e6)
	}
	if (totalSize >= 1e3) {
		return fmt.Sprintf("%.2fKB", gconv.Float32(totalSize)/1e3)
	}
	return fmt.Sprintf("%.2fB", gconv.Float32(totalSize))
}
